package other;

import javax.swing.*;
import java.awt.*;

public class DButton extends JButton {

    public DButton(String text) {
        super(text);
        init();
        setBackground(new Color(42, 130, 228));
    }

    public DButton(ImageIcon icon) {
        super(icon);
//        System.out.print(icon.getIconWidth()+"."+icon.getIconHeight()+"\n");
        setSize(icon.getIconWidth(),icon.getIconHeight());
        setBackground(new Color(0, 0, 0,0));
        init();
    }

    private void init() {
        setContentAreaFilled(false);
        setBorderPainted(false);
        setOpaque(false);
        setForeground(Color.white);
        setFont(new Font("宋体", Font.BOLD, 48));
    }

    public void setFontSize(int size) {
        setFont(new Font("宋体", Font.BOLD, size));
    }

    // 绘制按钮内容
    protected void paintComponent(Graphics g) {

        Graphics2D g2d = (Graphics2D) g;
        // 提高绘画精度
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setColor(getBackground());
        g2d.fillRoundRect(0, 0, getSize().width - 1, getSize().height - 1, 15, 15); // 设置圆角
        super.paintComponent(g);    //使用父类函数绘制一个焦点框
    }
}
